package cn.dapeng.tree.binary;

/**
 * @BelongProject study
 * @BelongPackage cn.dapeng.tree.binary
 * @Description TODO
 * @ClassName PathNum
 * @Author xupenghong
 * @Date 2024/9/20 10:43
 * @Version 1.0.0
 * <p>
 * 测试链接：https://leetcode.com/problems/path-sum
 */
public class PathNum {

    static boolean isSum = false;

    public static boolean hasPathSum(TreeNode head, int num) {
        if (head == null) {
            return false;
        }
        isSum = false;
        process1(head, 0, num);
        return isSum;
    }

    /**
     * @param x      :   遍历的节点
     * @param preNum :   遍历过的所有节点的和
     * @param num    :   路径和
     */
    public static void process1(TreeNode x, int preNum, int num) {
        if (x.left == null && x.right == null) {
            isSum = (preNum + x.val == num);
            if (isSum) {
                return;
            }
        }

        if (x.left != null) {
            process1(x.left, preNum + x.val, num);
        }

        if (x.right != null) {
            process1(x.right, preNum + x.val, num);
        }
    }

    /**
     *
     * @param x
     * @param rest
     * @param num
     */
    public static void process(TreeNode x, int rest, int num) {


    }
    public static void main(String[] args) {
        TreeNode head = new TreeNode(4);
        head.left = new TreeNode(1);
        head.right = new TreeNode(3);
        head.left.left = new TreeNode(2);

        System.out.println(hasPathSum(head, 7));
    }

}
